﻿#include "lgapackagemanager.h"
#include <QSettings>
#include <QCoreApplication>
#include <QFileInfo>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QDebug>

//#define STRING_FLUFF "fluff"
#define STRING_BYPASS "byPass"
#define STRING_SUB_DAMAGED "subStrateDamaged"
#define STRING_PAD_DAMAGED "padDamaged"
#define STRING_CAP_DAMAGED "capDamaged"
#define STRING_ONLY_SAVEFAIL "onlySaveFail"
#define STRING_MINI_AREA_RATIO "miniAreaRatio"
#define STRING_MINI_SUB_AREA "miniChippingArea"
#define STRING_MINI_SUB_WIDTH "miniChippingWidth"
#define STRING_MINI_SUB_HEIGHT "miniChippingHeight"
#define STRING_MINI_COUNT_LARGE "miniCountOfLarge"
#define STRING_DISCOLOR "discolor"
#define STRING_DISCOLOR_ON "discolorOn"
#define STRING_CHECK_LEVEL "checkLevel"
#define STRING_SAVE_DAYS "saveDays"
#define STRING_SEPERATE_FOLDER "seperateFolder"
#define STRING_SEPERATE_FOLDER_TEST_COUNT "seperateFolderTestCount"

#define STRING_CAP_MINI_AREA_RATIO "capMiniAreaRatio"
#define STRING_X_CNT "xCnt"
#define STRING_Y_CNT "yCnt"
#define STRING_X_OFFSET "xOffset"
#define STRING_Y_OFFSET "yOffset"

LgaPackageManager::LgaPackageManager()
{
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "Config");
	m_type = app.value("Package").toString();
}

//LgaPackageManager::PACKAGE_INFO LgaPackageManager::GetPackageInfo()
//{
//	PACKAGE_INFO def = GetPackageInfoFromFile();
//	QString appName = QCoreApplication::applicationName();
//	QString str = QString("%1\\Config").arg(appName);
//
//	QSettings app(QSettings::NativeFormat, QSettings::UserScope, str, m_type);
//
//	if (app.contains("lightPad1"))
//	{
//		def.lightConfigPad.brightness1 = app.value("lightPad1").toInt();
//	}
//	if (app.contains("lightPad2"))
//	{
//		def.lightConfigPad.brightness2 = app.value("lightPad2").toInt();
//	}
//
//	if (app.contains("lightCap1"))
//	{
//		def.lightConfigCap.brightness1 = app.value("lightCap1").toInt();
//	}
//	if (app.contains("lightCap2"))
//	{
//		def.lightConfigCap.brightness2 = app.value("lightCap2").toInt();
//	}
//
//	if (app.contains("lightSub1"))
//	{
//		def.lightConfigSubstrate.brightness1 = app.value("lightSub1").toInt();
//	}
//	if (app.contains("lightSub2"))
//	{
//		def.lightConfigSubstrate.brightness2 = app.value("lightSub2").toInt();
//	}
//
//	return def;
//}
//
//LgaPackageManager::PACKAGE_INFO LgaPackageManager::GetPackageInfoFromFile()
//{
//	QString appName = QCoreApplication::applicationName();
//	QSettings app1(appName, "Config");
//	QString chipType = app1.value("ChipType").toString();
//	PACKAGE_INFO def;
//	QString path = QString("%1/Config/package/%3/%2.cfg").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
//	QFile file(path);
//
//	if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
//		QXmlStreamReader reader(&file);
//
//		while (!reader.atEnd()) {
//
//			QXmlStreamReader::TokenType nType = reader.readNext();
//
//			switch (nType) {
//			case QXmlStreamReader::StartElement: {
//				QString strElementName = reader.name().toString();
//				//  qDebug()<<strElementName;
//
//				if (strElementName.compare("chip", Qt::CaseInsensitive) == 0) {
//					QXmlStreamAttributes attributes = reader.attributes();
//					//QString name, value;
//					if (attributes.hasAttribute("padNumbers")) {
//						def.padNumbers = attributes.value("padNumbers").toInt();
//					}
//					if (attributes.hasAttribute("capNumbers")) {
//						def.capNumbers = attributes.value("capNumbers").toInt();
//					}
//				/*	if (attributes.hasAttribute("erosionWidth")) {
//						def.erosionWidth = attributes.value("erosionWidth").toDouble();
//					}
//					if (attributes.hasAttribute("erosionHeight")) {
//						def.erosionHeight = attributes.value("erosionHeight").toInt();
//					}*/
//					if (attributes.hasAttribute("multiErrorCode")) {
//						def.multiErrorCode = attributes.value("multiErrorCode").toInt();
//					}
//				}
//				else if (strElementName.compare("lightConfigPad", Qt::CaseInsensitive) == 0) {
//					QXmlStreamAttributes attributes = reader.attributes();
//					if (attributes.hasAttribute("brightness1")) {
//						def.lightConfigPad.brightness1 = attributes.value("brightness1").toInt();
//					}
//					if (attributes.hasAttribute("brightness2")) {
//						def.lightConfigPad.brightness2 = attributes.value("brightness2").toInt();
//					}
//					if (attributes.hasAttribute("catchProp")) {
//						def.lightConfigPad.catchProp = attributes.value("catchProp").toInt();
//					}
//				}
//				else if (strElementName.compare("lightConfigCap", Qt::CaseInsensitive) == 0) {
//					QXmlStreamAttributes attributes = reader.attributes();
//					if (attributes.hasAttribute("brightness1")) {
//						def.lightConfigCap.brightness1 = attributes.value("brightness1").toInt();
//					}
//					if (attributes.hasAttribute("brightness2")) {
//						def.lightConfigCap.brightness2 = attributes.value("brightness2").toInt();
//					}
//					if (attributes.hasAttribute("catchProp")) {
//						def.lightConfigCap.catchProp = attributes.value("catchProp").toInt();
//					}
//				}
//				else if (strElementName.compare("lightConfigSub", Qt::CaseInsensitive) == 0) {
//					QXmlStreamAttributes attributes = reader.attributes();
//					if (attributes.hasAttribute("brightness1")) {
//						def.lightConfigSubstrate.brightness1 = attributes.value("brightness1").toInt();
//					}
//					if (attributes.hasAttribute("brightness2")) {
//						def.lightConfigSubstrate.brightness2 = attributes.value("brightness2").toInt();
//					}
//					if (attributes.hasAttribute("catchProp")) {
//						def.lightConfigSubstrate.catchProp = attributes.value("catchProp").toInt();
//					}
//				}
//				else if (strElementName.compare("pitch", Qt::CaseInsensitive) == 0) {
//					QXmlStreamAttributes attributes = reader.attributes();
//					if (attributes.hasAttribute(STRING_X_CNT)) {
//						def.xCnt = attributes.value(STRING_X_CNT).toInt();
//					}
//					if (attributes.hasAttribute(STRING_Y_CNT)) {
//						def.yCnt = attributes.value(STRING_Y_CNT).toInt();
//					}
//					if (attributes.hasAttribute(STRING_X_OFFSET)) {
//						def.xOffset = attributes.value(STRING_X_OFFSET).toInt();
//					}
//					if (attributes.hasAttribute(STRING_Y_OFFSET)) {
//						def.yOffset = attributes.value(STRING_Y_OFFSET).toInt();
//					}
//				}
//				break;
//			}
//
//			default:
//				break;
//			}
//		}
//
//		if (reader.hasError()) {
//			qDebug() << QString("err:%1  line:%2  column:%3  pos:%4").arg(reader.errorString()).arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.characterOffset());
//		}
//
//		file.close();
//	}
//	return def;
//}

void LgaPackageManager::SavePodToFile(POD_INFO p)
{
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString chipType = app1.value("ChipType").toString();
	QString path = QString("%1/Config/package/%3/%2.pod").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
	QFile file(path);
	if (!file.open(QFile::WriteOnly | QFile::Text)) {
		qDebug() << "Error: Cannot write file: " << qPrintable(file.errorString());
		return;
	}
	QXmlStreamWriter xmlWriter(&file);
	xmlWriter.setAutoFormatting(true);
	xmlWriter.writeStartDocument();

	xmlWriter.writeStartElement("property");

	xmlWriter.writeAttribute("rowPadCenter1", QString("%1").arg(p.padsCenter1.x()));
	xmlWriter.writeAttribute("columnPadCenter1", QString("%1").arg(p.padsCenter1.y()));
	xmlWriter.writeAttribute("rowPadCenter2", QString("%1").arg(p.padsCenter2.x()));
	xmlWriter.writeAttribute("columnPadCenter2", QString("%1").arg(p.padsCenter2.y()));
	xmlWriter.writeAttribute("rowPadCenter3", QString("%1").arg(p.padsCenter3.x()));
	xmlWriter.writeAttribute("columnPadCenter3", QString("%1").arg(p.padsCenter3.y()));
	xmlWriter.writeAttribute("rowPadCenter4", QString("%1").arg(p.padsCenter4.x()));
	xmlWriter.writeAttribute("columnPadCenter4", QString("%1").arg(p.padsCenter4.y()));

	xmlWriter.writeAttribute("rowCapCenter1", QString("%1").arg(p.capsCenter1.x()));
	xmlWriter.writeAttribute("columnCapCenter1", QString("%1").arg(p.capsCenter1.y()));

	foreach(QPointF point, p.padsPosList1) {
		xmlWriter.writeStartElement("padPos1");
		xmlWriter.writeAttribute("row", QString("%1").arg(point.x()));
		xmlWriter.writeAttribute("column", QString("%1").arg(point.y()));
		xmlWriter.writeEndElement();
	}

	foreach(QPointF point, p.padsPosList2) {
		xmlWriter.writeStartElement("padPos2");
		xmlWriter.writeAttribute("row", QString("%1").arg(point.x()));
		xmlWriter.writeAttribute("column", QString("%1").arg(point.y()));
		xmlWriter.writeEndElement();
	}

	foreach(QPointF point, p.padsPosList3) {
		xmlWriter.writeStartElement("padPos3");
		xmlWriter.writeAttribute("row", QString("%1").arg(point.x()));
		xmlWriter.writeAttribute("column", QString("%1").arg(point.y()));
		xmlWriter.writeEndElement();
	}

	foreach(QPointF point, p.padsPosList4) {
		xmlWriter.writeStartElement("padPos4");
		xmlWriter.writeAttribute("row", QString("%1").arg(point.x()));
		xmlWriter.writeAttribute("column", QString("%1").arg(point.y()));
		xmlWriter.writeEndElement();
	}


	foreach(QPointF point, p.capsPosList1) {
		xmlWriter.writeStartElement("capPos1");
		xmlWriter.writeAttribute("row", QString("%1").arg(point.x()));
		xmlWriter.writeAttribute("column", QString("%1").arg(point.y()));
		xmlWriter.writeEndElement();
	}

	xmlWriter.writeEndElement();

	xmlWriter.writeEndDocument();
	file.close();
	if (file.error()) {
		qDebug() << "Error: Cannot write file: "
			<< qPrintable(file.errorString());
		return;
	}
}

LgaPackageManager::POD_INFO LgaPackageManager::GetPodFromFile()
{
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString chipType = app1.value("ChipType").toString();
	POD_INFO def;
	QString path = QString("%1/Config/package/%3/%2.pod").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
	QFile file(path);

	if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		QXmlStreamReader reader(&file);

		while (!reader.atEnd()) {

			QXmlStreamReader::TokenType nType = reader.readNext();

			switch (nType) {
			case QXmlStreamReader::StartElement: {
				QString strElementName = reader.name().toString();
				//  qDebug()<<strElementName;

				if (strElementName.compare("property", Qt::CaseInsensitive) == 0) {
					QXmlStreamAttributes attributes = reader.attributes();


					if (attributes.hasAttribute("rowPadCenter1")) {
						def.padsCenter1.setX(attributes.value("rowPadCenter1").toDouble());
					}
					if (attributes.hasAttribute("columnPadCenter1")) {
						def.padsCenter1.setY(attributes.value("columnPadCenter1").toDouble());
					}
					if (attributes.hasAttribute("rowPadCenter2")) {
						def.padsCenter2.setX(attributes.value("rowPadCenter2").toDouble());
					}
					if (attributes.hasAttribute("columnPadCenter2")) {
						def.padsCenter2.setY(attributes.value("columnPadCenter2").toDouble());
					}

					if (attributes.hasAttribute("rowPadCenter3")) {
						def.padsCenter3.setX(attributes.value("rowPadCenter3").toDouble());
					}
					if (attributes.hasAttribute("columnPadCenter3")) {
						def.padsCenter3.setY(attributes.value("columnPadCenter3").toDouble());
					}
					if (attributes.hasAttribute("rowPadCenter4")) {
						def.padsCenter4.setX(attributes.value("rowPadCenter4").toDouble());
					}
					if (attributes.hasAttribute("columnPadCenter4")) {
						def.padsCenter4.setY(attributes.value("columnPadCenter4").toDouble());
					}


					if (attributes.hasAttribute("rowCapCenter1")) {
						def.capsCenter1.setX(attributes.value("rowCapCenter1").toDouble());
					}
					if (attributes.hasAttribute("columnCapCenter1")) {
						def.capsCenter1.setY(attributes.value("columnCapCenter1").toDouble());
					}
				

				}
				else if (QString::compare(strElementName, "padPos1") == 0) {  // 根元素
				//    qDebug() << QString("********** 开始元素<emptyPin> ********** ");
					QXmlStreamAttributes attributes = reader.attributes();
					QPointF point;
					if (attributes.hasAttribute("row")) {
						point.setX(attributes.value("row").toDouble());
					}
					if (attributes.hasAttribute("column")) {
						point.setY(attributes.value("column").toDouble());
					}

					def.padsPosList1.append(point);
				}
				else if (QString::compare(strElementName, "padPos2") == 0) {  // 根元素
				//    qDebug() << QString("********** 开始元素<emptyPin> ********** ");
					QXmlStreamAttributes attributes = reader.attributes();
					QPointF point;
					if (attributes.hasAttribute("row")) {
						point.setX(attributes.value("row").toDouble());
					}
					if (attributes.hasAttribute("column")) {
						point.setY(attributes.value("column").toDouble());
					}

					def.padsPosList2.append(point);
				}
				else if (QString::compare(strElementName, "padPos3") == 0) {  // 根元素
				//    qDebug() << QString("********** 开始元素<emptyPin> ********** ");
					QXmlStreamAttributes attributes = reader.attributes();
					QPointF point;
					if (attributes.hasAttribute("row")) {
						point.setX(attributes.value("row").toDouble());
					}
					if (attributes.hasAttribute("column")) {
						point.setY(attributes.value("column").toDouble());
					}

					def.padsPosList3.append(point);
				}
				else if (QString::compare(strElementName, "padPos4") == 0) {  // 根元素
				//    qDebug() << QString("********** 开始元素<emptyPin> ********** ");
					QXmlStreamAttributes attributes = reader.attributes();
					QPointF point;
					if (attributes.hasAttribute("row")) {
						point.setX(attributes.value("row").toDouble());
					}
					if (attributes.hasAttribute("column")) {
						point.setY(attributes.value("column").toDouble());
					}

					def.padsPosList4.append(point);
				}
				else if (QString::compare(strElementName, "capPos1") == 0) {  // 根元素
				//    qDebug() << QString("********** 开始元素<emptyPin> ********** ");
					QXmlStreamAttributes attributes = reader.attributes();
					QPointF point;
					if (attributes.hasAttribute("row")) {
						point.setX(attributes.value("row").toDouble());
					}
					if (attributes.hasAttribute("column")) {
						point.setY(attributes.value("column").toDouble());
					}

					def.capsPosList1.append(point);
				}
				
				break;
			}

			default:
				break;
			}
		}

		if (reader.hasError()) {
			qDebug() << QString("err:%1  line:%2  column:%3  pos:%4").arg(reader.errorString()).arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.characterOffset());
		}

		file.close();
	}


	return def;
}


void LgaPackageManager::SaveParamToFile(LgaPackageManager::INSPECT_PARAM param)
{
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString chipType = app1.value("ChipType").toString();
	QString path = QString("%1/Config/package/%3/%2.param").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
	QFile file(path);
	if (!file.open(QFile::WriteOnly | QFile::Text)) {
		qDebug() << "Error: Cannot write file: " << qPrintable(file.errorString());
		return;
	}
	QXmlStreamWriter xmlWriter(&file);
	xmlWriter.setAutoFormatting(true);
	xmlWriter.writeStartDocument();

	xmlWriter.writeStartElement("property");

	xmlWriter.writeAttribute(STRING_BYPASS, QString("%1").arg(param.bBypass));
	xmlWriter.writeAttribute(STRING_ONLY_SAVEFAIL, QString("%1").arg(param.bOnlySaveFail));
	xmlWriter.writeAttribute(STRING_PAD_DAMAGED, QString("%1").arg(param.bPadDamaged));
	xmlWriter.writeAttribute(STRING_CAP_DAMAGED, QString("%1").arg(param.bCapDamaged));
	xmlWriter.writeAttribute(STRING_SUB_DAMAGED, QString("%1").arg(param.bChipping));
	xmlWriter.writeAttribute(STRING_MINI_AREA_RATIO, QString("%1").arg(param.miniAreaRatio));
	xmlWriter.writeAttribute(STRING_MINI_SUB_AREA, QString("%1").arg(param.miniChippingArea));
	xmlWriter.writeAttribute(STRING_MINI_SUB_WIDTH, QString("%1").arg(param.miniChippingWidth));
	xmlWriter.writeAttribute(STRING_MINI_SUB_HEIGHT, QString("%1").arg(param.miniChippingHeight));
	

	xmlWriter.writeAttribute(STRING_MINI_COUNT_LARGE, QString("%1").arg(param.miniCountOfLarge));
	xmlWriter.writeAttribute(STRING_DISCOLOR, QString("%1").arg(param.discolorValue));
	xmlWriter.writeAttribute(STRING_DISCOLOR_ON, QString("%1").arg(param.bDisColor));
	xmlWriter.writeAttribute(STRING_SAVE_DAYS, QString("%1").arg(param.saveDays));
	xmlWriter.writeAttribute(STRING_SEPERATE_FOLDER, QString("%1").arg(param.bSeperateFolder));
	xmlWriter.writeAttribute(STRING_SEPERATE_FOLDER_TEST_COUNT, QString("%1").arg(param.iCountForSeperateFolder));

	xmlWriter.writeAttribute(STRING_CAP_MINI_AREA_RATIO, QString("%1").arg(param.capMiniAreaRatio));


	xmlWriter.writeEndElement();

	xmlWriter.writeEndDocument();
	file.close();
	if (file.error()) {
		qDebug() << "Error: Cannot write file: "
			<< qPrintable(file.errorString());
		return;
	}
}

LgaPackageManager::INSPECT_PARAM LgaPackageManager::GetParamFromFile()
{
	INSPECT_PARAM param;
	param.bPadDamaged = param.bChipping = param.bCapDamaged = true;
	param.bBypass = false;
	param.bDisColor = true;

	param.discolorValue = 8;
	param.miniChippingArea = 300;
	param.miniChippingWidth = 18;
	param.miniChippingHeight = 18;
	param.miniCountOfLarge = 15;
	param.miniAreaRatio = 25;
	param.bOnlySaveFail = false;
	param.bSeperateFolder = false;
	param.iCountForSeperateFolder = 100;

	param.capMiniAreaRatio = 20;
	QString appName = QCoreApplication::applicationName();
	QSettings app1(appName, "Config");
	QString chipType = app1.value("ChipType").toString();
	QString path = QString("%1/Config/package/%3/%2.param").arg(QCoreApplication::applicationDirPath()).arg(m_type).arg(chipType);
	QFile file(path);

	if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		QXmlStreamReader reader(&file);

		while (!reader.atEnd()) {
			QXmlStreamReader::TokenType nType = reader.readNext();
			switch (nType) {
			case QXmlStreamReader::StartElement: {
				QString strElementName = reader.name().toString();
				if (strElementName.compare("property", Qt::CaseInsensitive) == 0) {
					QXmlStreamAttributes attributes = reader.attributes();

					if (attributes.hasAttribute(STRING_BYPASS)) {
						param.bBypass = attributes.value(STRING_BYPASS).toInt();
					}
					if (attributes.hasAttribute(STRING_ONLY_SAVEFAIL)) {
						param.bOnlySaveFail = attributes.value(STRING_ONLY_SAVEFAIL).toInt();
					}
					if (attributes.hasAttribute(STRING_SAVE_DAYS)) {
						param.saveDays = attributes.value(STRING_SAVE_DAYS).toInt();
					}

					if (attributes.hasAttribute(STRING_CAP_DAMAGED)) {
						param.bCapDamaged = attributes.value(STRING_CAP_DAMAGED).toInt();
					}
					if (attributes.hasAttribute(STRING_SUB_DAMAGED)) {
						param.bChipping = attributes.value(STRING_SUB_DAMAGED).toInt();
					}
					if (attributes.hasAttribute(STRING_PAD_DAMAGED)) {
						param.bPadDamaged = attributes.value(STRING_PAD_DAMAGED).toInt();
					}
					if (attributes.hasAttribute(STRING_DISCOLOR)) {
						param.discolorValue = attributes.value(STRING_DISCOLOR).toInt();
					}
					if (attributes.hasAttribute(STRING_DISCOLOR_ON)) {
						param.bDisColor = attributes.value(STRING_DISCOLOR_ON).toInt();
					}

					if (attributes.hasAttribute(STRING_MINI_AREA_RATIO)) {
						param.miniAreaRatio = attributes.value(STRING_MINI_AREA_RATIO).toInt();
					}
					if (attributes.hasAttribute(STRING_MINI_SUB_AREA)) {
						param.miniChippingArea = attributes.value(STRING_MINI_SUB_AREA).toDouble();
					}
					if (attributes.hasAttribute(STRING_MINI_SUB_WIDTH)) {
						param.miniChippingWidth = attributes.value(STRING_MINI_SUB_WIDTH).toDouble();
					}
					if (attributes.hasAttribute(STRING_MINI_SUB_HEIGHT)) {
						param.miniChippingHeight = attributes.value(STRING_MINI_SUB_HEIGHT).toDouble();
					}
					if (attributes.hasAttribute(STRING_MINI_COUNT_LARGE)) {
						param.miniCountOfLarge = attributes.value(STRING_MINI_COUNT_LARGE).toInt();
					}

					if (attributes.hasAttribute(STRING_SEPERATE_FOLDER)) {
						param.bSeperateFolder = attributes.value(STRING_SEPERATE_FOLDER).toInt();
					}
					if (attributes.hasAttribute(STRING_SEPERATE_FOLDER_TEST_COUNT)) {
						param.iCountForSeperateFolder = attributes.value(STRING_SEPERATE_FOLDER_TEST_COUNT).toInt();
					}

					if (attributes.hasAttribute(STRING_CAP_MINI_AREA_RATIO)) {
						param.capMiniAreaRatio = attributes.value(STRING_CAP_MINI_AREA_RATIO).toInt();
					}

					
				}

				break;
			}

			default:
				break;
			}
		}

		if (reader.hasError()) {
			qDebug() << QString("err:%1  line:%2  column:%3  pos:%4").arg(reader.errorString()).arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.characterOffset());
		}

		file.close();
	}

	return param;
}